package com.visikard.utility;

import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public final class UILImageUtils {

	public static DisplayImageOptions createDisplayOption(int outputWidth, int roundType, int roundPercent,
			int shadowPercent, int shadowResId, int ratio, int defaultLoadingId) {
		// DisplayImageOptions options = new DisplayImageOptions.Builder()
		// .showImageOnLoading(defaultDrawable)
		// .cacheInMemory(true)
		// .cacheOnDisk(true)
		// .resetViewBeforeLoading(true)
		// .bitmapConfig(Bitmap.Config.RGB_565)
		// .imageScaleType(ImageScaleType.EXACTLY)
		// .postProcessor(new MyPostProcessorBitmap(ratio, roundType, roundPercent, shadowPercent, shadowResId))
		// .displayer(new MyCustomBitmapDisplayer(context, ratio, roundType, roundPercent, shadowPercent, shadowResId,
		// outputWidth))
		// .build();
		Builder bd = new Builder();
		if (defaultLoadingId > 0)
			bd.showImageOnLoading(defaultLoadingId);
		bd.cacheInMemory(true);
		bd.cacheOnDisk(true);
		// bd.resetViewBeforeLoading(true);
		if (outputWidth > 0) {
			bd.imageScaleType(ImageScaleType.EXACTLY);
		}
		if (ratio > 0 || roundType != BitmapUtils.ROUND_NONE || shadowPercent > 0) {
			bd.postProcessor(new MyPostProcessorBitmap(ratio, roundType, roundPercent, shadowPercent, shadowResId, 0, 0));
		}
		return bd.build();
	}

	public static DisplayImageOptions createDisplayOption(int outputWidth, int outputHeight, int roundType,
			int roundPercent, int shadowPercent, int shadowResId, int ratio, int defaultLoadingId) {
		Builder bd = new Builder();
		if (defaultLoadingId > 0)
			bd.showImageOnLoading(defaultLoadingId);
		bd.cacheInMemory(true);
		bd.cacheOnDisk(true);
		if (ratio > 0 || roundType != BitmapUtils.ROUND_NONE || shadowPercent > 0
				|| (outputWidth > 0 && outputHeight > 0)) {
			bd.postProcessor(new MyPostProcessorBitmap(ratio, roundType, roundPercent, shadowPercent, shadowResId,
					outputWidth, outputHeight));
		}
		return bd.build();
	}

	public static void loadImage(ImageView imageView, String url, int outputWidth, int roundType, int roundPercent,
			int shadowPercent, int shadowResId, int ratio, int defautLoadingId) {
		// call loadImage iwth default shadow vertical resource
		DisplayImageOptions option = createDisplayOption(outputWidth, roundType, roundPercent, shadowPercent,
				shadowResId, ratio, defautLoadingId);
		com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, imageView, option);
	}

	public static void loadImage(ImageView imageView, String url, int outputWidth, int outputHeight, int roundType,
			int roundPercent, int shadowPercent, int shadowResId, int ratio, int defautLoadingId) {
		// call loadImage iwth default shadow vertical resource
		DisplayImageOptions option = createDisplayOption(outputWidth, outputHeight, roundType, roundPercent,
				shadowPercent, shadowResId, ratio, defautLoadingId);
		com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, imageView, option);
	}

	public static void loadImageCallback(String url, int outputWidth, int roundType, int roundPercent,
			int shadowPercent, int shadowResId, int ratio, ImageLoadingListener imageListener) {

		DisplayImageOptions option = createDisplayOption(outputWidth, roundType, roundPercent, shadowPercent,
				shadowResId, ratio, 0);
		com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(url, option, imageListener);
	}

	public static void loadImageForChat(ImageView imageView, String url, int outputWidth, int roundType,
			int roundPercent, int shadowPercent, int shadowResId, int ratio, int defautLoadingId) {
		Builder bd = new Builder();
		if (defautLoadingId > 0)
			bd.showImageOnLoading(defautLoadingId);
		bd.cacheInMemory(true);
		bd.cacheOnDisk(false);
		if (outputWidth > 0) {
			bd.imageScaleType(ImageScaleType.EXACTLY);
		}
		if (ratio > 0 || roundType != BitmapUtils.ROUND_NONE || shadowPercent > 0) {
			bd.postProcessor(new MyPostProcessorBitmap(ratio, roundType, roundPercent, shadowPercent, shadowResId, 0, 0));
		}
		DisplayImageOptions option = bd.build();
		com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, imageView, option);
	}

}
